package com.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;


public class JdbcUtils {
//    private static DruidDataSource dataSource;
//
//    static {
//        try {
//            Properties properties = new Properties();
//            //读取jdbc.properties属性配置文件
//            InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("../jdbc.properties");
//            //从流中加载数据
//            properties.load(inputStream);
//            //创建 数据库连接 池
//            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
//
//            System.out.println(dataSource.getConnection());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    private static String url;
    private static String user;
    private static String password;
    private  static String driver;

    /**
     * 文件读取，只会执行一次，使用静态代码块
     */
    static {
        //读取文件，获取值
//        try {
//            //1.创建Properties集合类
//            Properties pro = new Properties();
//            //获取src路径下的文件--->ClassLoader类加载器
//            ClassLoader classLoader = JdbcUtils.class.getClassLoader();
//            URL resource = classLoader.getResource("jdbc.properties");;
//            String path = resource.getPath();
//            //2.加载文件
//            pro.load(new FileReader(path));
//            //3获取数据
//            url = pro.getProperty("url");
//            user = pro.getProperty("user");
//            password = pro.getProperty("password");
//            driver = pro.getProperty("driver");
//            System.out.println(url);
//            //4.注册驱动
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try {
            InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            driver = properties.getProperty("dirver");
            url = properties.getProperty("url");
            user = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args){

    }
    public static Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

    //关闭连接
    public static void close(ResultSet rs, Statement st,Connection conn){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(st != null){
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    }

